﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace RL.DataAccessLayer
{
    public class DBConn
    {
        private string _connectionString = "";
        private SqlConnection _connection = null;
        private SqlTransaction oSqlTransaction;

        public SqlConnection OpenConnection()
        {
            try
            {
                _connectionString = ConfigurationManager.ConnectionStrings["SMRDBConn"].ConnectionString;
                _connection = new SqlConnection(_connectionString);

                if (_connection.State != ConnectionState.Open)
                {
                    _connection.Open();
                }
            }
            catch (Exception ex)
            {
                string error = ex.Message;
            }
            return _connection;
        }

        public void CloseConnection()
        {
            try
            {
                if (_connection.State != ConnectionState.Closed)
                    _connection.Close();
            }
            catch (Exception ex)
            {
                string error = ex.Message;
            }
        }

        public void BeginTransaction(IsolationLevel isolationLevel)
        {
            oSqlTransaction = _connection.BeginTransaction(isolationLevel);
        }

        public void BeginTransaction()
        {
            oSqlTransaction = _connection.BeginTransaction();
        }

        public void CommitTransaction()
        {
            oSqlTransaction.Commit();
        }

        public void RollbackTransaction()
        {
            if (oSqlTransaction != null)
            {
                oSqlTransaction.Rollback();
            }
        }


        public SqlTransaction GetSqlTransaction()
        {
            return oSqlTransaction;
        }
    }
}
